package com.haierCamera.customapplication.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.RegisterRepo;
import com.haierCamera.customapplication.api.vo.GetUserInfoResponse;
import com.haierCamera.customapplication.api.vo.Token;
import com.haierCamera.customapplication.databinding.HzklActivityVcodeBinding;
import com.haierCamera.customapplication.ui.MainActivity;
import com.haierCamera.customapplication.utils.AppManager;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;
import com.haierCamera.customapplication.utils.android.SystemUIHelper;

import java.util.Timer;
import java.util.TimerTask;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLWXVcodeActivity extends BaseActivity {
    private HzklActivityVcodeBinding binding;
    private Timer timer = new Timer();
    private int time = 60;
    @Inject
    RegisterRepo registerRepo;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_vcode);
        initData();
    }

    private void initData() {
        String openId = getIntent().getStringExtra("openId");
        String phone = getIntent().getStringExtra("phone");
        binding.forgetBack.setOnClickListener(view -> finish());
        binding.vcodePhonne.setText(String.format("验证码已发送至手机号%s", phone));
        binding.vcodeMin.setOnClickListener(view -> {
            if (time != 60) {
                return;
            }
            sendTime();
            sendCode(phone);
        });
        binding.vcodeBtn.setOnClickListener(view -> {
            String vcode = binding.vcode.getText().toString();
            if (TextUtils.isEmpty(vcode)) {
                Toast.makeText(HZKLWXVcodeActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                return;
            }
            vodeSend(openId, phone, vcode);
        });
        sendTime();
    }

    private void vodeSend(String openId, String phone, String vcode) {
        registerRepo.bindingWX(openId, phone, vcode).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(HZKLVcodeActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    Token data = objectResource.data;
                    SharedPreferencesUtils.saveString(Constants.SP_USER_TOKEN, data.token);
                    //Toast.makeText(HZKLWXVcodeActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
                    getUser();
                    break;
            }
        });
    }

    private void sendCode(String phone) {
        registerRepo.getVcode(phone).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    Toast.makeText(HZKLWXVcodeActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    break;
                case SUCCESS:
                    Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();
                    break;
            }
        });
    }

    private void getUser() {
        registerRepo.getUserInfo().observe(this, getUserInfoResource -> {
            switch (getUserInfoResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    break;
                case SUCCESS:
                    GetUserInfoResponse data = getUserInfoResource.data;
                    assert data != null;
                    if (TextUtils.isEmpty(data.nickName)) {
//                        binding.phone.setText(data.mobile);
                        SharedPreferencesUtils.saveString(Constants.SP_USER_PHONE, data.mobile);
                    } else {
//                        binding.phone.setText(data.nickName);
                        SharedPreferencesUtils.saveString(Constants.SP_USER_PHONE, data.nickName);
                    }
                    SharedPreferencesUtils.saveString(Constants.SP_USER_HEAD_URL, data.headUrl);
                    startActivity(new Intent(HZKLWXVcodeActivity.this, MainActivity.class));
                    setResult(RESULT_OK);
                    finish();
//                    Glide.with(this).load(data.headUrl).apply(new RequestOptions().circleCrop()).into(binding.userIcon);
                    break;
            }
        });
    }

    private void sendTime() {
        MyTask timerTask = new MyTask();
        timer.schedule(timerTask, 100, 1000);
    }

    class MyTask extends TimerTask {

        @Override
        public void run() {
            --time;
            runOnUiThread(() -> binding.vcodeMin.setText(time + "s"));
            if (time == 0) {
                time = 60;
                this.cancel();
                runOnUiThread(() -> binding.vcodeMin.setText("重新获取验证码"));
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
